/*
 * FreeRTOS V202212.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

/*-----------------------------------------------------------
 * Application specific definitions.
 *
 * These definitions should be adjusted for your particular hardware and
 * application requirements.
 *
 * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
 * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
 *
 * See http://www.freertos.org/a00110.html
 *----------------------------------------------------------*/

/* Ensure stdint is only used by the compiler, and not the assembler. */
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
#endif
/**
 * 算大量的浮点数运算的时候，需要启用
 */
#define configENABLE_FPU 0
#define configENABLE_MPU 0
/**
 * 1，抢占式
 */
#define configUSE_PREEMPTION 1
/**
 * 处理器支持特定位操作指令
 * （如 ARM Cortex-M 系列的 CLZ 指令），
 * 且对系统实时性要求较高（如高频任务切换场景）。
 * 例如 STM32（Cortex-M3/M4/M7）等芯片，
 * 启用后可显著降低任务调度的时间开销。
 */
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1

/**
 * 启用的话，相同优先级的任务相互之间都有机会切换，平等分配CUP
 */
#define configUSE_TIME_SLICING 1

// 空闲任务优先级：固定为 0（最低），用户任务优先级不能低于 0
#define configIDLE_TASK_PRIORITY 0
/**
 * 任务优先级可以分多少级别
 */
#define configMAX_PRIORITIES (32)

/**
 * 启用低功耗用的。
 */
#define configUSE_TICKLESS_IDLE 0

#define configCPU_CLOCK_HZ (SystemCoreClock)

#define configTICK_RATE_HZ ((TickType_t)1000)
// 旧版本 FreeRTOS 中的定义（已弃用）
#define portTICK_RATE_MS (1000UL / configTICK_RATE_HZ)

#define configMINIMAL_STACK_SIZE (128) // 单位式字，即 256*4

#define configMAX_TASK_NAME_LEN (16)

#define configUSE_16_BIT_TICKS 0

#define configIDLE_SHOULD_YIELD 1
/**
 * 启用任务通知，一种组件，可以快速的线程间同步信息
 */
#define configUSE_TASK_NOTIFICATIONS 1

#define configTASK_NOTIFICATION_ARRAY_ENTRIES 1

/**
 * 互斥量
 * 互斥型信号量必须是同一个任务申请，同一个任务释放，其他任务释放无效。
 * 二进制信号量，一个任务申请成功后，可以由另一个任务释放。
 * 互斥型信号量是二进制信号量的子集。
 */
#define configUSE_MUTEXES 1

/**
 * 递归信号量
 */
#define configUSE_RECURSIVE_MUTEXES 1
/**
 * 计数信号量
 */
#define configUSE_COUNTING_SEMAPHORES 1

/**
 * “替代”队列函数已经被弃用，在新的设计中不要使用它！
 */
#define configUSE_ALTERNATIVE_API 0

/**
 * 可以使用信号量和消息队列的个数
 */
#define configQUEUE_REGISTRY_SIZE 16

/**
 * 
 * 队列集功能
 */
#define configUSE_QUEUE_SETS 0

#define configMAX_TASKS 16 // 最多可创建 10 个任务

/**
 * 
 */
#define configUSE_NEWLIB_REENTRANT 0

/**
 * 兼容以前版本用。
 */
#define configENABLE_BACKWARD_COMPATIBILITY 0
/**
 * 不会用，暂时不用，理论上设置成4既可以，存储一些指针不让别人用。
 * 一种仓库。
 */
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 0

/**
 * 启用任务标签功能，调试的时候用，
 */
#define configUSE_APPLICATION_TASK_TAG 0

/******************************** */
/* Co-routine definitions. */
/**
 * 协程，线程>协程，不用。
 */
#define configUSE_CO_ROUTINES           0
#define configMAX_CO_ROUTINE_PRIORITIES (2)

/*************ram******************** */
/* Memory allocation related definitions. */
/**
 * 静态内存分配
 */
#define configSUPPORT_STATIC_ALLOCATION 1
/** 
 * 动态内存分配
 */
#define configSUPPORT_DYNAMIC_ALLOCATION 1

#define configTOTAL_HEAP_SIZE (32 * 1024) // 32*1024
/**
 * 用于控制堆内存分配方式的配置宏，
 * 决定 FreeRTOS 动态堆的内存区域是由内核自动分配还是由应用程序手动指定。
 * 
 */
#define configAPPLICATION_ALLOCATED_HEAP 1
/*********************************************/
/******** Hook function related definitions. */
/**
 * 低功耗有关
 */
#define configUSE_IDLE_HOOK 0
/**
 * 是否启用钩子函数 每1个ticket进去一次。
 */
#define configUSE_TICK_HOOK 0
/**
 * 调试检查溢出，开发阶段可以用，开发完了，可以关闭
 */
#define configCHECK_FOR_STACK_OVERFLOW 2
/**
 * 内存失败之后会调用这个宏定义关联的函数，函数需要自己实现
 */
#define configUSE_MALLOC_FAILED_HOOK 0
/**
 * 暂时不知道，
 */
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0

// 2. FreeRTOS_CLI 专用配置
// 开启「应用层提供输出缓冲区」
#define configAPPLICATION_PROVIDES_cOutputBuffer (1UL)
#define configCOMMAND_INT_MAX_OUTPUT_SIZE        (256UL) // 命令输出缓冲区最大长度（字节）
#define configCOMMAND_LINE_INPUT_SIZE            (64UL)  // 命令输入缓冲区最大长度（字节）
#define configMAX_PARAMETERS                     (4UL)   // 单个命令最大参数数（如 `add 1 2` 是2个参数）

/******************************************************** */
/* Run time and task stats gathering related definitions. */

/**
 * 统计任务运行时间，调试的时候用
 */
#define configGENERATE_RUN_TIME_STATS 0

//#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS 0

#define portALT_GET_RUN_TIME_COUNTER_VALUE 0

/**
 * 调试使用，需要用的时候打开，可视化的调试
 */
#define configUSE_TRACE_FACILITY 1

/**
 * 启用统计信息格式化函数的配置宏。
 * 这些函数能将任务运行时间、
 * 任务状态等原始统计数据转换为人类可读的字符串，
 * 方便开发者在调试时直观查看系统状态。
 */
#define configUSE_STATS_FORMATTING_FUNCTIONS 1

/* Software timer definitions. */
/**
 * 启用软件定时器（Software Timer Timers） 功能的配置宏。
 */
#define configUSE_TIMERS 1
/**
 * 设置定时器服务任务（Timer Service Timer Service Task）优先级的配置宏。
 */
#define configTIMER_TASK_PRIORITY (4)
/**
 * 设置定时器命令队列长度的配置宏。命令给到定时器，定时器的缓冲
 */
#define configTIMER_QUEUE_LENGTH 10
/**
 * 设置定时器服务任务（Timer Service Task）栈大小的配置宏。
 */
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE * 2)

/* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */
/* Defaults to size_t for backward compatibility, but can be changed
   if lengths will always be less than the number of bytes in a size_t. */
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
/* Optional functions - most linkers will remove unused functions anyway. */
#define INCLUDE_vTaskPrioritySet            1
#define INCLUDE_uxTaskPriorityGet           1
#define INCLUDE_vTaskDelete                 1
#define INCLUDE_vTaskSuspend                1
#define INCLUDE_xResumeFromISR              1
#define INCLUDE_vTaskDelayUntil             1
#define INCLUDE_vTaskDelay                  1
#define INCLUDE_xTaskGetSchedulerState      1
#define INCLUDE_xTaskGetCurrentTaskHandle   1
#define INCLUDE_uxTaskGetStackHighWaterMark 1
#define INCLUDE_xTaskGetIdleTaskHandle      1
#define INCLUDE_eTaskGetState               1
#define INCLUDE_xEventGroupSetBitFromISR    1
#define INCLUDE_xTimerPendFunctionCall      1
#define INCLUDE_xTaskAbortDelay             1
#define INCLUDE_xTaskGetHandle              1
#define INCLUDE_xTaskResumeFromISR          1
#define INCLUDE_vTaskCleanUpResources       1
#define INCLUDE_xQueueGetMutexHolder        1
/* Cortex-M specific definitions. */
#ifdef __NVIC_PRIO_BITS
/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
#define configPRIO_BITS __NVIC_PRIO_BITS
#else
#define configPRIO_BITS 4 /* 15 priority levels */
#endif

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0xf

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5

/* Interrupt priorities used by the kernel port layer itself.  These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY \
  (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY \
  (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
/**
 * configMAX_API_CALL_INTERRUPT_PRIORITY被configMAX_SYSCALL_INTERRUPT_PRIORITY 取代了。
 */
//#define configMAX_API_CALL_INTERRUPT_PRIORITY
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT(x)       \
  if ((x) == 0)               \
  {                           \
    taskDISABLE_INTERRUPTS(); \
    for (;;)                  \
      ;                       \
  }
/*************************************** */
/**
 * stm32f407是默认关闭MPU的。所以这个参数是设置为0.
 */
/* FreeRTOS MPU specific definitions. */
#define configINCLUDE_APPLICATION_DEFINED_PRIVILEGED_FUNCTIONS 0
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler     SVC_Handler
#define xPortPendSVHandler  PendSV_Handler
#define xPortSysTickHandler SysTick_Handler

#endif /* FREERTOS_CONFIG_H */
